<?php
use Workerman\Connection\AsyncUdpConnection;
require_once __DIR__.'/../vendor/autoload.php';
$name='/source/ff.h264';
$path =__DIR__.'/demo/'.$name;
$port = "6278";
$worker_udp=new \Workerman\Worker('udp://0.0.0.0:'.$port);
$worker_udp->count=4;
//function worker_start(){
//    global $udp_connection;
//    $udp_connection = new \Workerman\Connection\AsyncTcpConnection('tcp://127.0.0.1:4002');
//    $udp_connection->connect();
//
////    global $worker_udp;
////    if($worker_udp->id==0){
////        global $name;
////        global $path;
////        \Workerman\Lib\Timer::add(20, function()use($name,$path)
////        {
////            $shell="ffmpeg -re -i ".$path." -an -vcodec copy -f flv rtmp://106.75.165.88:8298/live/123";
////            exec($shell,$res);
////        });
////    }
//}
function handle_message($connection, $message)
{
//    file_put_contents(__DIR__.'/hex',bin2hex($message).PHP_EOL,FILE_APPEND);
    file_put_contents(__DIR__.'/demo/source/dd.h264',substr($message,12),FILE_APPEND);

    //去除padding
//    $padding = substr($message,0,1);
//    $bin=($padding & 0b00100000)>>5;
//    if($bin==1){
//        $padding_len = substr($message,strlen($message)-1,1);
//        $padding_len = bindec($padding_len);
//        $message = substr($message,0,strlen($message)-1-$padding_len);
//    }
    //去除RTP头
//    $message = substr($message,12);

//        global $udp_connection;
//        $udp_connection->send($message);

//    global $name;
//    global $path;

//    $shell='ffmpeg -i 127:001  flv rmpt://nginx////';
//    $bit=$message[1];
//    $bin=($bit & 0b00100000)>5;
//    if($bin=1){
//        $len=strlen($message)-12-
//    }

}
//$worker_udp->onWorkerStart = 'worker_start';
$worker_udp->onMessage = 'handle_message';

if (!defined('GLOBAL_START')) {
    \Workerman\Worker::runAll();
}

